package rexsee.core.browser.clazz;

import java.util.HashMap;

import rexsee.core.browser.Browser;
import rexsee.core.browser.RexseeUrl;
import android.content.Context;

public final class PreloadFilterListener extends FilterListener {

	public static final String PRELOAD_FILTER_NAME = "preload";
	public static final String PRELOAD_FILTER_TYPE = "preload";

	private final HashMap<String, String> mMap;
	public PreloadFilterListener() {
		super(PRELOAD_FILTER_NAME, PRELOAD_FILTER_TYPE);
		mMap = new HashMap<String, String>();
	}
	@Override
	public void run(Context context, Browser browser, String url) {
		String code = mMap.get(url);
		if (code != null) {
			browser.loadDataWithBaseURL(url, code, "text/html", "UTF-8", url);
			browser.history.add(url);
		} else {
			browser.loadUrl(url);
		}
	}
	public void add(String url, String code) {
		addUrl(url);
		mMap.put(url, code);
	}
	public void remove(String url) {
		removeUrl(url);
		mMap.remove(url);
	}
	public void remove() {
		clearUrls();
		mMap.clear();
	}
	public void removeFirst() {
		remove(super.getUrl(0));
	}
	public String getCode(String url) {
		return mMap.get(url);
	}
	@Override
	public boolean containsUrl(String url) {
		url = RexseeUrl.cleanUrl_(url);
		for (int i = 0; i < urls.size(); i++) {
			if (url.equals(urls.get(i))) return true;
		}
		return false;
	}

}
